package com.xueqiu.lovepetmedicalmanagersystem.service.Impl;

import com.xueqiu.lovepetmedicalmanagersystem.mapper.OwnersMapper;
import com.xueqiu.lovepetmedicalmanagersystem.mapper.PetsMapper;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Owners;
import com.xueqiu.lovepetmedicalmanagersystem.pojo.Pets;
import com.xueqiu.lovepetmedicalmanagersystem.service.OwnersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xueqiu
 */
@Service
public class OwnersServiceImpl implements OwnersService {
    @Autowired
    private OwnersMapper ownersMapper;
    @Autowired
    private PetsMapper petsMapper;

    /**
     * 根据宠物主人ID查询单个宠物主人
     * @param ownId 宠物主人ID
     * @return 宠物主人对象；可能找不到为NULL
     */
    @Override
    public Owners queryOwnerById(Integer ownId) {
        return ownersMapper.queryOwnerById(ownId);
    }

    /**
     * 根据宠物主人姓名模糊查询多个宠物主人
     * @param ownName 宠物主人姓名关键词
     * @return 符合关键词的宠物主人对象列表；可能找不到为EMPTY
     */
    @Override
    public List<Owners> queryOwnerLikeName(String ownName) {
        List<Owners> ownersList = ownersMapper.queryOwnerLikeName(ownName);
        for (Owners owners : ownersList) {
            List<Pets> petsList = petsMapper.queryPetByOwnId(owners.getId());
            owners.setPetsList(petsList);
        }
        return ownersList;
    }

    /**
     * 根据宠物主人姓名查询单个宠物主人；前提是宠物主人姓名无重复
     * @param ownName 宠物主人姓名
     * @return 宠物主人对象；可能找不到为NULL
     */
    @Override
    public Owners queryOwnerByName(String ownName) {
        return ownersMapper.queryOwnerByName(ownName);
    }

    /**
     * 根据宠物主人ID更新单个数据
     * @param owners 数据源，包含宠物主人ID
     * @return 更新条数
     */
    @Override
    public Integer ownUpdate(Owners owners) {
        return ownersMapper.updateOwn(owners);
    }

    /**
     * 新增单个宠物主人数据
     * @param owners 数据源，不包含宠物主人ID
     * @return 新增条数
     */
    @Override
    public Integer insertOwn(Owners owners) {
        return ownersMapper.insertOwn(owners);
    }

    /**
     * 查询所有宠物主人
     * @return 宠物主人对象列表；可能找不到为EMPTY
     */
    @Override
    public List<Owners> getAllOwners() {
        return ownersMapper.queryAllOwners();
    }

}
